//
//  PxTextField.m
//  Express
//
//  Created by 泰然财富 on 15/6/17.
//  Copyright (c) 2015年 泰然财富. All rights reserved.
//

#import "PxButtonSwitch.h"
#import "UIView+Sizes.h"
#import "PxGlobal.h"

@implementation PxButtonSwitch

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initViews:frame];
    }
    return self;
}

-(void) initViews:(CGRect)frame
{
    CGFloat height = CGRectGetHeight(frame);
    CGFloat width = CGRectGetWidth(frame);
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.tag = 1001;
    self.leftBtn = btn1;
    [btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
    btn1.frame = CGRectMake(0, 0, width/2, height);
    [btn1 setBackgroundImage:[PxGlobal imageWithColor:[UIColor colorWithHTMLStr:@"56a0e1"]] forState:UIControlStateSelected];
    [btn1 setBackgroundImage:[PxGlobal imageWithColor:[UIColor colorWithHTMLStr:@"ffffff"]] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor colorWithHTMLStr:@"ffffff"] forState:UIControlStateSelected];
    [btn1 setTitleColor:[UIColor colorWithHTMLStr:@"666666"] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn1 setTitle:@"在家"];
    [self addSubview:btn1];
    btn1.selected = YES;
    [self setHalfRadius:btn1];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(width/2, 0, 1, height)];
    [lineView setBackgroundColor:[UIColor colorWithHTMLStr:@"cccccc"]];
    [self addSubview:lineView];
    
    UIButton *btn2 = [PxGlobal duplicate:btn1];
    btn2.tag = 1002;
    self.rightBtn = btn2;
    [btn2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
    [btn2 setTitle:@"不在家"];
    btn2.left = btn1.right;
    [self addSubview:btn2];
    btn2.selected = NO;
    [self setHalfRadius:btn2];
    
    self.layer.borderColor = [UIColor colorWithHTMLStr:@"cccccc"].CGColor;
    self.layer.borderWidth = 1;
    self.layer.cornerRadius = 4;
}

-(void)setHalfRadius:(UIView *) view
{
    UIBezierPath *maskPath;
    if (view.tag == 1002) {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(4, 4)];
    } else {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(4, 4)];
    }
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

-(void)click:(UIButton *) btn
{
    self.rightBtn.selected = !self.rightBtn.selected;
    self.leftBtn.selected = !self.leftBtn.selected;
    if (self.rightBtn.selected) {
        self.homeStatus = NotAtHome;
    } else if (self.leftBtn.selected) {
        self.homeStatus = AtHome;
    }
    if (self.delegate) {
        [self.delegate buttonSwitchClick:self.homeStatus];
    }
}

-(void)setDefaultStatus:(HomeStatus)status
{
    if (status == AtHome) {
        self.leftBtn.selected = YES;
        self.rightBtn.selected = NO;
    } else {
        self.leftBtn.selected = NO;
        self.rightBtn.selected = YES;
    }
}

@end